document.getElementById("register").onclick = function() {
    let flag = 1;
    let data = new FormData();
    data.append("username", document.getElementById("username").value);
    data.append("password", document.getElementById("password").value);
    data.append("myname", document.getElementById("myname").value);
    data.append("phone", document.getElementById("phone").value);
    var error_name = document.getElementById("error_name");
    var error_pwd = document.getElementById("error_pwd");
    var error_myname = document.getElementById("error_myname");
    var error_phone = document.getElementById("error_phone");



    //用户名校验
    function verifyName() {
        var verifyName = document.getElementById("username").value;
        var test_name = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
        if (!test_name.test(verifyName)) {
            error_name.innerHTML = "警告：用户名需为字符串类型！！";
            isNotError = false;
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    }
    verifyName();

    //密码校验
    function verifyPwd() {
        var verifyPwd = document.getElementById("password").value;
        var test_pwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
        if (!test_pwd.test(verifyPwd)) {
            error_pwd.innerHTML = "警告：密码格式错误！！";
            isNotError = false;
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    }
    verifyPwd();

    //姓名校验
    function verifyMyname() {
        var verifyMyname = document.getElementById("myname").value;
        var test_myname = /^[a-zA-Z]{4,30}$/; //只能为英文，4-30位
        if (!test_myname.test(verifyMyname)) {
            error_myname.innerHTML = "警告：用户名格式错误！！";
            isNotError = false;
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    }
    verifyMyname();

    //电话校验
    function verifyPhone() {
        var verifyPhone = document.getElementById("phone").value;
        var test_phone = /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/; //手机号码，3-4位区号，7-8位直播号码，1－4位分机号
        if (!test_phone.test(verifyPhone)) {
            error_phone.innerHTML = "警告：号码格式错误！！";
            isNotError = false;
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    }
    verifyPhone();

    // 1.创建异步对象
    let xhr1 = new XMLHttpRequest();

    // 设置请求url
    let url = "http://www.rushmc.top/api/register";
    //打开链接
    if (flag == 1) {
        xhr1.open("POST", url, true);
        //发送请求
        xhr1.send(data);
        xhr1.onreadystatechange = function() {
            // 处理服务器返回的参数，更新当前页面
            if (xhr1.readyState === 4 && xhr1.status === 200) {
                window.sessionStorage.visitor = 0;
                let res1 = JSON.parse(xhr1.responseText);
                if (res1.code === 200) {
                    console.log(res1.msg);
                    window.location.href = "login.html";
                } else {
                    console.log(res1.msg);
                }
            }
        }
    }
}
document.querySelector('.short').addEventListener('click', function() {
    window.sessionStorage.visitor = 1;
})